package com.ncloud.monitor.zabbix4j.proxy;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.monitor.zabbix4j.ZabbixApiException;
import com.ncloud.monitor.zabbix4j.ZabbixApiMethod;

/**
 * @author Suguru Yajima
 */
public class Proxy extends ZabbixApiMethod {

    public Proxy(String apiUrl, String auth) {
        super(apiUrl, auth);
    }

    public ProxyCreateResponse create(ProxyCreateRequest request) throws ZabbixApiException {
        ProxyCreateResponse response = null;
        request.setAuth(auth);

        JSONObject requestJson = JSON.parseObject(JSON.toJSONString(request));

        try {
            String responseJson = sendRequest(requestJson);

            response = JSON.parseObject(responseJson, ProxyCreateResponse.class);
        } catch (ZabbixApiException e) {
            throw new ZabbixApiException(e);
        }

        return response;
    }

    public ProxyDeleteResponse create(ProxyDeleteRequest request) throws ZabbixApiException {
        ProxyDeleteResponse response = null;
        request.setAuth(auth);

        JSONObject requestJson = JSON.parseObject(JSON.toJSONString(request));

        try {
            String responseJson = sendRequest(requestJson);

            response = JSON.parseObject(responseJson, ProxyDeleteResponse.class);
        } catch (ZabbixApiException e) {
            throw new ZabbixApiException(e);
        }

        return response;
    }

    public ProxyDeleteResponse delete(ProxyDeleteRequest request) throws ZabbixApiException {
        ProxyDeleteResponse response = null;
        request.setAuth(auth);

        JSONObject requestJson = JSON.parseObject(JSON.toJSONString(request));

        try {
            String responseJson = sendRequest(requestJson);

            response = JSON.parseObject(responseJson, ProxyDeleteResponse.class);
        } catch (ZabbixApiException e) {
            throw new ZabbixApiException(e);
        }

        return response;
    }

    public ProxyUpdateResponse update(ProxyUpdateRequest request) throws ZabbixApiException {
        ProxyUpdateResponse response = null;
        request.setAuth(auth);

        JSONObject requestJson = JSON.parseObject(JSON.toJSONString(request));

        try {
            String responseJson = sendRequest(requestJson);

            response = JSON.parseObject(responseJson, ProxyUpdateResponse.class);
        } catch (ZabbixApiException e) {
            throw new ZabbixApiException(e);
        }

        return response;
    }

    public ProxyGetResponse get(ProxyGetRequest request) throws ZabbixApiException {
        ProxyGetResponse response = null;
        request.setAuth(auth);

        JSONObject requestJson = JSON.parseObject(JSON.toJSONString(request));

        try {
            String responseJson = sendRequest(requestJson);

            response = JSON.parseObject(responseJson, ProxyGetResponse.class);
        } catch (ZabbixApiException e) {
            throw new ZabbixApiException(e);
        }

        return response;
    }
}
